package net.melon.order.inst.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.esj.basic.dao.querybuilder.QueryBuilder;
import net.esj.basic.exception.MelonException;
import net.esj.basic.service.impl.DefaultServiceImpl;
import net.esj.basic.utils.DateFormat;
import net.esj.basic.utils.DateUtils;
import net.esj.basic.utils.Pagination;
import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.Validators;
import net.melon.order.inst.service.OrderService;
import net.melon.order.pojo.CarLease;
import net.melon.order.pojo.Order;
import net.melon.order.pojo.Order.OrderState;
import net.melon.sso.pojo.Member;
import net.melon.stock.inst.service.StockService;

@Service("orderService")
public class OrderServiceImpl extends DefaultServiceImpl implements OrderService {
	
	@Resource
	private StockService stockService;

	@Override
	synchronized
	public String saveOrder(Order order) {
		order.setId(getOrderId());
		order.setGmtCreate(DateUtils.now());
		order.setState(OrderState.NO_PAY.getState());
		save(order);
		return order.getId();
	}

	private String getOrderId() {
		QueryBuilder<Order> qb = createQueryBuilder(Order.class);
		Object o = queryMaxByQueryBuilder(qb, "id");
		if(!Validators.isEmpty(o)){
			String id = "";
			if(o instanceof String){
				id = (String)o;
			}
			int number = Integer.valueOf(id.substring(8, 16));
			return id.substring(0, 8) + StringUtils.addParamToPreStr("0", 8, ""+(number + 1));
		}else{
			String id = DateFormat.formateDateTime(DateUtils.now(), "yyyyMMdd");
			return id + "00000001";
		}
	}

	@Override
	public Pagination<Order> getPagination(Pagination<Order> pagination) {
		return queryPageByQueryBuilder(pagination);
	}

	@Override
	public List<Order> getOrderByMember(Member member) {
		QueryBuilder<Order> qb = createQueryBuilder(Order.class);
		qb.eq("member", member).notEq("state", OrderState.DELETE.getState());
		return queryListByQueryBuilder(qb);
	}

	@Override
	public void saveCarLease(CarLease carLease) {
		carLease.setGmtCreate(DateUtils.now());
		save(carLease);
	}

	@Override
	public Order getOrderById(String orderId) {
		Order order = get(orderId, Order.class);
		return order;
	}

	@Transactional
	@Override
	public void updatePayOrder(Order order) {
		order.setState(OrderState.NO_PUT_CAR.getState());
		order.setPayTime(DateUtils.now());
		stockService.reduceStockByOrder(order);
		update(order);
		
	}

	@Override
	public void putCar(String orderId) {
		Order order = getOrderById(orderId);
		if(order.getState()==OrderState.NO_PUT_CAR.getState()){
			order.setState(OrderState.PUT_CAR.getState());
			order.setPutTime(DateUtils.now());
			update(order);
		}else{
			throw new MelonException("订单状态错误！");
		}
		
	}

	@Override
	public void remarks(String id, String remarks) {
		Order order = getOrderById(id);
		if(order!=null){
			order.setRemarks(remarks);
			update(order);
		}
	}
}
